﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;

namespace FacialClub.Helpers
{
    /// <summary>
    /// HttpRequestBaseExtensions
    /// </summary>
    public static class HttpRequestBaseExtensions
    {
        /// <summary>
        /// GetUserIpAddress
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static IPAddress GetUserIpAddress(this HttpRequestBase request)
        {
            IPAddress address;

            if (!IPAddress.TryParse(request.UserHostAddress, out address))
            {
                address = null;
            }

            return address;
        }

        /// <summary>
        /// GenerateAntiForgeryToken
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key"></param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string GenerateAntiForgeryToken(this HttpRequestBase request, string key, string salt)
        {
            return (key + salt + request.UserAgent).ComputeHash();
        }
    }
}